package com.woniuxy.service.controller;

import com.woniuxy.commons.entity.Escort;
import com.woniuxy.commons.entity.Order;
import com.woniuxy.commons.entity.OrderKey;
import com.woniuxy.commons.util.ResponseResult;
import com.woniuxy.commons.util.ResultStatus;
import com.woniuxy.service.mapper.EscortMapper;
import com.woniuxy.service.service.OrderService;
import com.woniuxy.service.utils.PageMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
@CrossOrigin
public class OrderController {
    @Resource
    private OrderService orderService;
    @Resource
    private EscortMapper escortMapper;

    @RequestMapping("/all/{page}/{size}")
    public ResponseResult all(@PathVariable("page") int page,@PathVariable("size") int size){
        log.info("访问订单方法");
        PageMessage all=orderService.all(page,size);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setMsg("获取所有订单成功");
        responseResult.setCode(200);
        responseResult.setStatus(ResultStatus.SUCCESS);
        responseResult.setData(all);
        return  responseResult;

    }
    @RequestMapping("/allescort")
    public ResponseResult allescort(){
        List<Escort> all=escortMapper.all();
        ResponseResult responseResult=new ResponseResult();
        responseResult.setData(all);
        responseResult.setCode(200);
        responseResult.setMsg("获取所有陪护人成功");
        responseResult.setStatus(ResultStatus.SUCCESS);
        return responseResult;
    }

    //转单
    @RequestMapping("/turn/{orderId}/{escortId}")
    public ResponseResult turn(@PathVariable("orderId")int orderId,@PathVariable("escortId")int escortId){
        boolean turn=orderService.turn(orderId,escortId);
        ResponseResult responseResult=new ResponseResult();
        if(turn){
            responseResult.setStatus(ResultStatus.SUCCESS);
            responseResult.setMsg("转单成功");
            responseResult.setCode(200);
            responseResult.setData(null);
        }
        else{
            responseResult.setStatus(ResultStatus.FAIL);
            responseResult.setMsg("转单失败");
            responseResult.setCode(400);
            responseResult.setData(null);
        }
        return responseResult;
    }

    //关闭订单
    @RequestMapping("/close/{orderId}")
    public ResponseResult close(@PathVariable("orderId")int orderId){
        boolean close= orderService.close(orderId);
        ResponseResult responseResult=new ResponseResult();
        if(close){
            responseResult.setStatus(ResultStatus.SUCCESS);
            responseResult.setMsg("关闭订单成功");
            responseResult.setCode(200);
            responseResult.setData(null);
        }
        else{
            responseResult.setStatus(ResultStatus.FAIL);
            responseResult.setMsg("关闭订单失败");
            responseResult.setCode(400);
            responseResult.setData(null);
        }
        return responseResult;
    }
    //多条件查询
    @RequestMapping("/find/{page}/{size}")
    public ResponseResult find(@PathVariable("page")int page, @PathVariable("size")int size, @RequestBody OrderKey orderkey){
        log.info("访问筛选方法");
        ResponseResult responseResult=new ResponseResult();
        String no=orderkey.getNo();
        String escort=orderkey.getEscort();
        String name=orderkey.getName();
        int status=orderkey.getStatus();
        log.info("no:"+no);
        log.info("escort:"+escort);
        log.info("name:"+name);
        log.info("status:"+status);
        PageMessage all=orderService.findbyNoAndNameNndStatus(page,size,no,name,escort,status);
            responseResult.setStatus(ResultStatus.SUCCESS);
            responseResult.setMsg("筛选订单成功");
            responseResult.setCode(200);
            responseResult.setData(all);
        return  responseResult;
    }

    @RequestMapping("/disabled/{disabledId}")
    public ResponseResult disabled(@PathVariable("disabledId")int disabledId){
        List<Order> all=orderService.disabled(disabledId);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setMsg("获取所有订单成功");
        responseResult.setCode(200);
        responseResult.setStatus(ResultStatus.SUCCESS);
        responseResult.setData(all);
        return  responseResult;
    }

    @RequestMapping("/closebydisabled/{orderId}")
    public ResponseResult closebydisabled(@PathVariable("orderId")int orderId){
        boolean close=orderService.closebydisabled(orderId);
        ResponseResult responseResult=new ResponseResult();
        if(close){
            responseResult.setStatus(ResultStatus.SUCCESS);
            responseResult.setMsg("取消订单成功");
            responseResult.setCode(200);
            responseResult.setData(null);
        }
        else{
            responseResult.setStatus(ResultStatus.FAIL);
            responseResult.setMsg("取消订单失败");
            responseResult.setCode(400);
            responseResult.setData(null);
        }
        return responseResult;
    }
    //残疾人用户端查找
    @RequestMapping("findbydisabled/{disabledId}/{key}")
    public ResponseResult findbydisabled(@PathVariable("disabledId")int disabledId,@PathVariable("key")String key){
        List<Order> all=orderService.findbydisabled(key,disabledId);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setMsg("获取所有订单成功");
        responseResult.setCode(200);
        responseResult.setStatus(ResultStatus.SUCCESS);
        responseResult.setData(all);
        return  responseResult;
    }

    //陪护人用户端查找
    @RequestMapping("findbyescortId/{escortId}/{key}")
    public ResponseResult findbyescortId(@PathVariable("escortId")int escortId,@PathVariable("key")String key){
        List<Order> all=orderService.findbyescortId(key,escortId);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setMsg("获取所有订单成功");
        responseResult.setCode(200);
        responseResult.setStatus(ResultStatus.SUCCESS);
        responseResult.setData(all);
        return  responseResult;
    }

    @RequestMapping("/revisit/{orderId}")
    public ResponseResult revisit(@PathVariable("orderId")int orderId){
        boolean revisit= orderService.revisit(orderId);
        ResponseResult responseResult=new ResponseResult();
        if(revisit){
            responseResult.setStatus(ResultStatus.SUCCESS);
            responseResult.setMsg("回访成功");
            responseResult.setCode(200);
            responseResult.setData(null);
        }
        else{
            responseResult.setStatus(ResultStatus.FAIL);
            responseResult.setMsg("回访失败");
            responseResult.setCode(400);
            responseResult.setData(null);
        }
        return responseResult;
    }
   //评论
    @RequestMapping("/review/{orderId}/{review}")
    public ResponseResult review(@PathVariable("orderId")int orderId,@PathVariable("review")String review){
        orderService.review(review,orderId);
        ResponseResult responseResult=new ResponseResult();

            responseResult.setStatus(ResultStatus.SUCCESS);
            responseResult.setMsg("评论成功");
            responseResult.setCode(200);
            responseResult.setData(null);

        return responseResult;
    }
    @RequestMapping("/escort/{escortId}")
    public ResponseResult escort(@PathVariable("escortId")int escortId){
        List<Order> all=orderService.escort(escortId);
        ResponseResult responseResult=new ResponseResult();
        responseResult.setMsg("获取所有订单成功");
        responseResult.setCode(200);
        responseResult.setStatus(ResultStatus.SUCCESS);
        responseResult.setData(all);
        return  responseResult;
    }
    @RequestMapping("/acceptbyescort/{orderId}")
    public ResponseResult accept(@PathVariable("orderId")int orderId){
        boolean accept=orderService.accept(orderId);
        ResponseResult responseResult=new ResponseResult();
        if(accept){
            responseResult.setStatus(ResultStatus.SUCCESS);
            responseResult.setMsg("接单成功");
            responseResult.setCode(200);
            responseResult.setData(null);
        }
        else{
            responseResult.setStatus(ResultStatus.FAIL);
            responseResult.setMsg("接单失败");
            responseResult.setCode(400);
            responseResult.setData(null);
        }
        return responseResult;
    }

    @RequestMapping("/refusebyescort/{orderId}")
    public ResponseResult refuse(@PathVariable("orderId")int orderId){
        boolean refuse=orderService.refuse(orderId);
        ResponseResult responseResult=new ResponseResult();
        if(refuse){
            responseResult.setStatus(ResultStatus.SUCCESS);
            responseResult.setMsg("陪护人取消成功");
            responseResult.setCode(200);
            responseResult.setData(null);
        }
        else{
            responseResult.setStatus(ResultStatus.FAIL);
            responseResult.setMsg("陪护人取消失败");
            responseResult.setCode(400);
            responseResult.setData(null);
        }
        return responseResult;
    }

}
